package com.lyj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyj.pojo.Department;
import com.lyj.pojo.Employee;
import com.lyj.pojo.Job;
import com.lyj.pojo.LayuiPage;
import com.lyj.service.DepartmentService;
import com.lyj.service.EmployeeService;
import com.lyj.service.JobService;
import com.lyj.util.LayuiData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private JobService jobService;

    @RequestMapping("/list.html")
    public ModelAndView toList(ModelAndView mv){
        mv.setViewName("employeeList");
        mv.addObject("jobs", jobService.findAllJob(new Job()));
        mv.addObject("depts", departmentService.findAllDept(new Department()));
        return mv;
    }

    @RequestMapping("/list")
    public ResponseEntity list(LayuiPage layuiPage, Employee employee){
        PageHelper.startPage(layuiPage.getPage(),layuiPage.getLimit());
        List<Employee> emps = employeeService.findAllEmp(employee);
        for (Employee emp : emps) {
            Job jobById = jobService.findJobById(emp.getJobId());
            emp.setJobName(jobById.getName());
            Department deptById = departmentService.findDeptById(emp.getDeptId());
            emp.setDeptName(deptById.getName());
        }

        return ResponseEntity.ok(LayuiData.toLayuiTableModel(new PageInfo(emps)));
    }

    @RequestMapping("/update.html")
    public ModelAndView toUpdate(ModelAndView mv, Integer id){
        mv.addObject("emp",employeeService.findEmpById(id));
        mv.addObject("depts", departmentService.findAllDept(new Department()));
        mv.addObject("jobs", jobService.findAllJob(new Job()));
        mv.setViewName("employeeUpdate");
        return mv;
    }

    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody Employee employee){
        List<Employee> allEmp = employeeService.findAllEmp(new Employee());
        for (Employee emp : allEmp) {
            if (emp.getPhone().equals(employee.getPhone()) && emp.getId() != employee.getId()){
                return ResponseEntity.ok("该手机号码已存在");
            }
            if (emp.getTel().equals(employee.getTel()) && emp.getId() != employee.getId()){
                return ResponseEntity.ok("该电话号码已存在");
            }
            if (emp.getCardId().equals(employee.getCardId()) && emp.getId() != employee.getId()){
                return ResponseEntity.ok("该身份证号码已存在");
            }
            if (emp.getEmail().equals(employee.getEmail()) && emp.getId() != employee.getId()){
                return ResponseEntity.ok("该邮箱已存在");
            }
            if (emp.getQq().equals(employee.getQq()) && emp.getId() != employee.getId()){
                return ResponseEntity.ok("该QQ账号已存在");
            }
        }
        employeeService.updateEmpById(employee);
        return ResponseEntity.ok("1");
    }

    @RequestMapping("/addEmp.html")
    public String toAdd(Model model){
        model.addAttribute("jobs",jobService.findAllJob(new Job()));
        model.addAttribute("depts",departmentService.findAllDept(new Department()));
        return "addEmployee";
    }

    @RequestMapping("/addEmp")
    public ResponseEntity add(@RequestBody Employee employee){
        List<Employee> allEmp = employeeService.findAllEmp(new Employee());
        for (Employee emp : allEmp) {
            if (emp.getPhone().equals(employee.getPhone())){
                return ResponseEntity.ok("该手机号码已存在");
            }
            if (emp.getTel().equals(employee.getTel())){
                return ResponseEntity.ok("该电话号码已存在");
            }
            if (emp.getCardId().equals(employee.getCardId())){
                return ResponseEntity.ok("该身份证号码已存在");
            }
            if (emp.getEmail().equals(employee.getEmail())){
                return ResponseEntity.ok("该邮箱已存在");
            }
            if (emp.getQq().equals(employee.getQq())){
                return ResponseEntity.ok("该QQ账号已存在");
            }
        }
        employeeService.addEmp(employee);
        return ResponseEntity.ok("2");
    }

    @RequestMapping("/delete/{id}")
    public ResponseEntity delete(@PathVariable Integer id){
        employeeService.deleteEmpById(id);
        return ResponseEntity.ok("1");
    }


}
